/*
Source : https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Author : nflush@outlook.com
Date   : 2016-07-05
*/
/*
108. Convert Sorted Array to Binary Search Tree
Total Accepted: 80660 Total Submissions: 211905 Difficulty: Medium

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Subscribe to see which companies asked this question
*/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    TreeNode * setTreeNode(vector<int>& nums, int low, int high){
        if (low > high) return NULL;
        int mid = (high - low + 1)/2 + low;
        TreeNode *node = new TreeNode(nums[mid]);
        node->left = setTreeNode(nums, low, mid-1);
        node->right = setTreeNode(nums, mid+1, high);
        return node;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return setTreeNode(nums, 0, nums.size()-1);
    }
};

